package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallAdMapper;
import org.linlinjava.litemall.db.domain.LitemallAd;
import org.linlinjava.litemall.db.domain.LitemallAdExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class AdvertisingServiceImpl implements AdvertisingService {

    @Autowired
    LitemallAdMapper mapper;

    @Override
    public List<LitemallAd> getAdPage(String title,String content,Integer page,Integer pageSize,String order) {
        LitemallAdExample example = new LitemallAdExample();
        //构造自定义查询条件
        LitemallAdExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause("id "+order);
        PageHelper.startPage(page,pageSize);
        criteria.andDeletedEqualTo(false);
        if(title != null){
            criteria.andNameLike('%'+title+'%');
        }
        if(content != null) {
            criteria.andContentLike('%'+content+'%');
        }
        return mapper.selectByExample(example);
    }

    @Override
    public boolean delete(LitemallAd ad) {
        LitemallAdExample example = new LitemallAdExample();
        LitemallAdExample.Criteria criteria= example.createCriteria();
        ad.setDeleted(true);
        return mapper.updateByPrimaryKey(ad) > 0 ? true : false;
    }

    @Override
    public boolean add(LitemallAd ad) {
        return mapper.insert(ad) > 0 ? true : false;
    }

    @Override
    public boolean modify(LitemallAd ad) {
        return mapper.updateByPrimaryKey(ad) > 0 ? true : false;
    }

    @Override
    public List<LitemallAd> queryIndex() {
        LitemallAdExample example = new LitemallAdExample();
        example.or().andPositionEqualTo((byte) 1).andDeletedEqualTo(false).andEnabledEqualTo(true);
        return mapper.selectByExample(example);
    }

}
